define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {

        index: function () {

            // 初始化表格参数配置

            Table.api.init({

                extend: {

                    index_url: 'shopro/banner/index' + location.search,

                    add_url: 'shopro/banner/add',

                    edit_url: 'shopro/banner/edit',

                    del_url: 'shopro/banner/del',

                    table: 'shopro_banner',

                }

            });

            var table = $("#table");

            // 初始化表格

            table.bootstrapTable({

                url: $.fn.bootstrapTable.defaults.extend.index_url,

                pk: 'id',

                sortName: 'weigh',

				pagination: false,

				// escape: false,

                columns: [

                    [

                        {checkbox: true},

                        {field: 'id', title: __('Id')},

						{field: 'name', title: __('Name'), align: 'left', formatter: Controller.api.formatter.escape2Html},

						{field: 'category_name',title: __('分类'),formatter: Table.api.formatter.category_id,operate: "in",formatter: Table.api.formatter.search},
                        {field: 'image', title: __('图片'), events: Table.api.events.image, formatter: Table.api.formatter.image},
						{field: 'weigh', title: __('Weigh')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},

						{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]

                ],

				search: false,

				commonSearch: false

            });


            // 为表格绑定事件

            Table.api.bindevent(table);

        },
        select: function () {

            // 初始化表格参数配置

            Table.api.init({

                extend: {

                    index_url: 'shopro/banner/select',

                }

            });

            var urlArr = [];

        

            var table = $("#table");

        

            table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {

                if (e.type == 'check' || e.type == 'uncheck') {

                    row = [row];

                } else {

                    urlArr = [];

                }

                $.each(row, function (i, j) {

                    if (e.type.indexOf("uncheck") > -1) {

                        var index = urlArr.indexOf(j.route);

                        if (index > -1) {

                            urlArr.splice(index, 1);

                        }

                    } else {

                        urlArr.indexOf(j.route) == -1 && urlArr.push(j.route);

                    }

                });

            });

        

            // 初始化表格

            table.bootstrapTable({

                url: $.fn.bootstrapTable.defaults.extend.index_url,

                sortName: 'id',

                pagination: false,

                // escape: false,

                columns: [

                    [

                        {checkbox: true},

        				{field: 'id', title: __('Id')},

						{field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},

						{field: 'type',title: __('Type'),custom: {article: 'info', goods: 'success'},formatter: Table.api.formatter.flag},

        				{field: 'name', title: __('Name'), align: 'left', formatter: Controller.api.formatter.escape2Html},

						{field: 'id',title: __('Route'), operate: false, formatter: Controller.api.formatter.route},

                        {

                            field: 'operate', title: __('Operate'), events: {

                                'click .btn-chooseone': function (e, value, row, index) {

                                    var multiple = Backend.api.query('multiple');

                                    multiple = multiple == 'true' ? true : false;

                                    Fast.api.close({url: '/pages/product/list?data=' + encodeURIComponent(JSON.stringify({"category_id":row.id,"category_name":row.name.replace(row.spacer,'')})), name: row.name.replace(row.spacer,""), multiple: multiple});

                                },

                            }, formatter: function () {

                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';

                            }

                        }

                    ]

                ]

            });

            // 选中多个

            $(document).on("click", ".btn-choose-multi", function () {

                var multiple = Backend.api.query('multiple');

                multiple = multiple == 'true' ? true : false;

                Fast.api.close({url: urlArr.join(","), multiple: multiple});

            });

            // 为表格绑定事件

            Table.api.bindevent(table);

        },

		add: function () {

            Controller.api.bindevent();

        },

		create: function () {

		    Controller.api.bindevent();

		},

        edit: function () {

            Controller.api.bindevent();

        },

        api: {

			formatter: {

				subnode: function (value, row, index) {

				    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '

				        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';

				},

				route: function (value, row, index) {

					return '/pages/product/list?data=' + {"category_id":row.id,"category_name":row.name.replace(row.spacer,'')};

				},

				//转意符换成普通字符

				escape2Html: function (value, row, index) {

					return value.toString().replace(/(&|&amp;)nbsp;/g, '');

				}

			},

            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));

            }

        }

    };

    return Controller;

});